package com.example.demo.controller;

import com.example.demo.entity.Location.LocationDTO;
import com.example.demo.entity.Product.FilesDTO;
import com.example.demo.entity.Product.Units;
import com.example.demo.entity.Product.WorkProcedure;
import com.example.demo.entity.ProductBillDetail;
import com.example.demo.entity.produceRepairDetailDTO;
import com.example.demo.services.LocationService;
import com.example.demo.services.ProductBillDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/productBillDetail")
public class ProductBillDetailController {

    @Autowired
    private ProductBillDetailService service;
    @Autowired
    private LocationService locationService;

    //分页显示数据(web端)
    @PostMapping("/detail")
    public ResponseEntity<Page<ProductBillDetail>> getProductBillDetail(@RequestBody Map<String, Integer> payload) {
        int page = payload.getOrDefault("page", 0);
        int size = payload.getOrDefault("size", 10);
        Page<ProductBillDetail> pagedData = service.getProductBillDetail(page, size);
        return ResponseEntity.ok(pagedData);
    }

    @PostMapping("/fetchDetail")
    public List<ProductBillDetail> fetchDetail(@RequestBody Map<String, String> payload) {
        String urlData = payload.get("data");
        return service.fetchDetail(urlData);
    }

    /*工序获取中文*/
    @PostMapping("/getWpCn")
    public WorkProcedure getWpCn(@RequestBody Map<String, String> payload) {
        String wpCode = payload.get("wpCode");
        return service.getWpCn(wpCode);
    }

    @PostMapping("/getUnitCn")
    public Units getUnitCn(@RequestBody Map<String, String> payload) {
        String unitCode = payload.get("unitCode");
        return service.getUnitCn(unitCode);
    }

    @PostMapping("/addDetail")
    public void addDetail(@RequestBody produceRepairDetailDTO payload) {
        service.addDetail(payload);
    }

    @PostMapping("/updateDetail")
    public void updateDetail(@RequestBody produceRepairDetailDTO payload) {
        service.updateDetail(payload);
    }

    /*@PostMapping("/getGoods")
    public  List<> getGoods(@RequestBody )*/

    @PostMapping("/getStatus")
    public String getStatus(@RequestBody Map<String, String> payload) {
        String billNo = payload.get("billNo");
        System.out.println(billNo);
        return service.getStatus(billNo);
    }

    @PostMapping("/getGoodsPower")
    public boolean getGoodsPower(@RequestBody Map<String, String> payload) {
        String goodsCode = payload.get("goodsCode");
        String userID = payload.get("userID");
        return service.getGoodsPower(goodsCode, userID);
    }

    @PostMapping("/getFilesUrl")
    public FilesDTO getFilesUrl(@RequestBody Map<String, String> payload) {
        String goodsCode = payload.get("goodsCode");
        return service.getFilesUrl(goodsCode);
    }

    @PostMapping("/checkWpLast")
    public boolean checkWpLast(@RequestBody Map<String, String> payload) {
        String billNo = payload.get("billNo");
        return service.checkWpLast(billNo);
    }

    @PostMapping("/getLocation")
    public List<LocationDTO> getLocation(@RequestBody Map<String, String> payload) {
        String goodsCode = payload.get("goodsCode");
        System.out.println(service.getLocation(goodsCode));
        return service.getLocation(goodsCode);
    }

    @PostMapping("/updateLocation")
    public ResponseEntity<?> updateLocation(@RequestBody List<LocationDTO> locations) {
        try {
            System.out.println(locations);
            locationService.updateLocation(locations);
            return ResponseEntity.ok().build();
        }catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body("更新失败" + e.getMessage());
        }
    }
}

